//
//  RechargeRecordController.m
//  EasyToTesco2.0
//
//  Created by 杨森 on 16/10/4.
//  Copyright © 2016年 EasyToTesco. All rights reserved.
//

#import "RechargeRecordController.h"
#import "TopStatusView.h"
#import "RechargeCell.h"
#import "RechargeModel.h"

@interface RechargeRecordController ()<UITableViewDelegate,UITableViewDataSource>
{
    MBProgressHUD *progressHUD;
    NSUInteger _refreshIndex;
    UIImageView *noDrawImage;
}

@property(nonatomic,strong)UITableView *rechargeView;

@property(nonatomic,strong)NSMutableArray *rechargeArray;

@end

@implementation RechargeRecordController

static NSString *const RechargeRecordIdentifier = @"RechargeRecordIdentifier";

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _refreshIndex = 1;
    [self buildView];
    [self rechargeRecordListData:_refreshIndex];
    [self addPullUpdateData];
}

#pragma mark >>>>>>>>刷新
- (void)addPullUpdateData
{
    _rechargeView.mj_footer = [TopPullRefreshAnaimation footerWithRefreshingTarget:self refreshingAction:@selector(rechargeRecordListLoadMoreData)];
    
}

- (void)rechargeRecordListLoadMoreData
{
    _refreshIndex ++;
    [self rechargeRecordListData:_refreshIndex];
    [_rechargeView.mj_footer endRefreshing];
}

- (void)rechargeRecordListData:(NSInteger)refreshIndex
{
    progressHUD = [MBProgressHUD loadingInformationToView];
    [self.view addSubview:progressHUD];
    
    NSString *refreshIndexStr = [NSString stringWithFormat:@"%zd",refreshIndex];
    
    NSDictionary *params = @{@"username":USERNAME,@"p":refreshIndexStr};
    NSLog(@"%@",params);
    [[NetWorkSharedTools sharedNetWorkTools] objectWithURLDict:params AndMenthodName:@"/api/Order/moneyList" completeBlock:^(id object) {
        
        NSLog(@"%@",object);
        if (JudgeBackDataCode) {
            
            NSArray *dataArr = [object objectForKey:@"data"];
            for (NSDictionary *dict in dataArr) {
                RechargeModel *rechargeModel = [[RechargeModel alloc] initWithDictionary:dict error:nil];
                
                if (rechargeModel) {
                    [self.rechargeArray addObject:rechargeModel];
                }
            }
            
        }else{
            
            //订单没有
            if (refreshIndex == 1) {
                noDrawImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"noRecharge"]];
                [self.view addSubview:noDrawImage];
                
                [noDrawImage mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.centerX.equalTo(self.view.mas_centerX);
                    make.centerY.equalTo(self.view.mas_centerY).offset(-HEIGHT/8);
                    make.width.mas_equalTo(WIDTH/2);
                    make.height.mas_equalTo(HEIGHT/4);
                }];
                
                [_rechargeView removeFromSuperview];
                self.view.backgroundColor = BACKGROUND_COLOR;
                
            }else{
                
                //没有更多数据
                [_rechargeView.mj_footer endRefreshingWithNoMoreData];
                
            }
            
            
        }
        [progressHUD hide:YES];
        [_rechargeView reloadData];
        
        
    }];

}


#pragma mark 数据源
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.rechargeArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    RechargeCell *cell=[tableView dequeueReusableCellWithIdentifier:RechargeRecordIdentifier];
    
    if (cell==nil) {
        cell=[[RechargeCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:RechargeRecordIdentifier];
        
    }
    cell.selectionStyle =UITableViewCellSelectionStyleNone;
    RechargeModel *rechargeModel = self.rechargeArray[indexPath.section];
    cell.UserLabel.text = rechargeModel.username;
    cell.moneyLabel.text= rechargeModel.money;
    
    NSString *timeStr = rechargeModel.create_time;
    if (timeStr.length) {
        cell.UserLabel.text = [NSString stringWithFormat:@"%@",[timeStr substringToIndex:10]];
    } else {
        cell.UserLabel.text = @"";
    }
    
    
    cell.timeLabel.text = @"支付类型";
    
    if ([rechargeModel.status isEqualToString:@"0"]) {
        cell.statusLabel.text = @"失败";
    }else if ([rechargeModel.status isEqualToString:@"1"]){
        cell.statusLabel.text = @"成功";
    }else{
        
    }
    
    if ([rechargeModel.pay_type isEqualToString:@"3"]) {
        cell.timeLabel.text = @"支付宝";
    } else if ([rechargeModel.pay_type isEqualToString:@"4"]) {
        cell.timeLabel.text = @"微信";
    } else {
        cell.statusLabel.text = @"未付款";
        cell.timeLabel.text = @"未处理";
    }
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 3;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}

#pragma mark UI
- (void)buildView
{
    TopStatusView *topStatusView = [[TopStatusView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 50) titleArray:@[@"充值时间",@"充值金额",@"支付类型",@"状态"]];
    [self.view addSubview:topStatusView];
    
    self.rechargeView = [[UITableView alloc] initWithFrame:CGRectMake(0,50,WIDTH, HEIGHT-50-64) style:UITableViewStylePlain];
    [self.view addSubview:_rechargeView];
    _rechargeView.delegate = self;
    _rechargeView.dataSource = self;
    _rechargeView.backgroundColor = BACKGROUND_COLOR;
    _rechargeView.rowHeight = 45;
    [_rechargeView registerNib:[UINib nibWithNibName:@"RechargeCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:RechargeRecordIdentifier];
}

- (NSMutableArray *)rechargeArray
{
    if (!_rechargeArray) {
        _rechargeArray = [NSMutableArray array];
    }
    return _rechargeArray;
}


@end
